package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.PageData;
import com.woniu.entity.vo.LoginLogVO;
import com.woniu.entity.vo.ResentLoginLogVO;
import com.woniu.mapper.AdminMapper;
import com.woniu.pojo.Admin;
import com.woniu.pojo.LoginLog;
import com.woniu.mapper.LoginLogMapper;
import com.woniu.service.ILoginLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sss
 * @since 2022-12-26
 */
@Service
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper, LoginLog> implements ILoginLogService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private LoginLogMapper loginLogMapper;


    @Override
    public PageData listByPhone(Integer currentPage, Integer pageSize, String phone) {
        IPage<LoginLogVO> page = new Page<>(currentPage,pageSize);
        Admin admin = adminMapper.getByPhone(phone);
        IPage<LoginLogVO> loginLogVOIPage = loginLogMapper.listLoginLogVO(page, admin.getId());
        PageData pageData = new PageData(loginLogVOIPage.getTotal(),loginLogVOIPage.getRecords());

        return pageData;
    }

    /**
     * 查询最近的登录信息
     *
     * @param phone
     * @return ResentLoginLogVO
     * @author yemaf
     * @time 2022/12/29 14:44:30
     */
    @Override
    public ResentLoginLogVO resentLoginLogByPhone(String phone) {
        Admin admin = adminMapper.getByPhone(phone);
        String id = admin.getId();
        ResentLoginLogVO resentLoginLogVO = loginLogMapper.selectByAdminID(id);
        resentLoginLogVO.setPhone(phone);
        return resentLoginLogVO;
    }
}
